package com.craftplay.service;


import com.craftplay.dao.PlaceMapper;
import com.craftplay.dao.UnitMapper;
import com.craftplay.dao.UserMapper;
import com.craftplay.dao.UserTicketMapper;
import com.craftplay.model.Place;
import com.craftplay.model.User;
import com.craftplay.model.UserTicket;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PlaceMapper placeMapper;

    @Autowired
    private UserTicketMapper userTicketMapper;

    @Autowired
    private UnitMapper unitMapper;


    public Map<String, String> checkUserInfo(WxMpUser wxMpUser) {
        Map<String, String> map = new HashMap<String, String>();
        User user = userMapper.selectByOpenId(wxMpUser.getOpenId());
        if (user == null) {
            user = new User();
            user.setOpenId(wxMpUser.getOpenId());
            user.setCity(wxMpUser.getCity());
            user.setProvince(wxMpUser.getProvince());
            user.setCountry(wxMpUser.getCountry());
            user.setNickname(wxMpUser.getNickname());
            user.setHeadUrl(wxMpUser.getHeadImgUrl());
            user.setSex(wxMpUser.getSex());
            user.setSubscribe(wxMpUser.getSubscribe() ? 1 : 0);
            userMapper.insert(user);
        } else {
            //更新用户
            user.setOpenId(wxMpUser.getOpenId());
            user.setCity(wxMpUser.getCity());
            user.setProvince(wxMpUser.getProvince());
            user.setCountry(wxMpUser.getCountry());
            if (null == user.getNickname() || user.getNickname().equals("")) {
                user.setNickname(wxMpUser.getNickname());
            }
            user.setHeadUrl(wxMpUser.getHeadImgUrl());
            user.setSex(wxMpUser.getSex());
            user.setSubscribe(wxMpUser.getSubscribe() ? 1 : 0);
            userMapper.updateByPrimaryKey(user);
        }

        //可以删除
        user = userMapper.selectByOpenId(wxMpUser.getOpenId());
        String ticket = addTicket(user.getUserId());
        map.put("ticket", ticket);
        return map;
    }


    public String addTicket(int userId) {
        UserTicket userTicket = new UserTicket();
        userTicket.setUserId(userId);
        Date now = new Date();
        now.setTime(now.getTime() + 3600 * 24 * 100);
        userTicket.setExpired(now);
        userTicket.setStatus(0);
        userTicket.setTicket(UUID.randomUUID().toString().replaceAll("-", ""));
        userTicketMapper.insertUserTicket(userTicket);
        return userTicket.getTicket();
    }

    public int updateUserInfo(String feature, String tel, String openId, String nickname, Integer userType) {
        return userMapper.updateUserInfo(feature, tel, openId, nickname, userType);
    }

    public int updateIntro(String intro, String introImgUrl, String openId) {
        return userMapper.updateIntro(intro, introImgUrl, openId);
    }

    public String getUserIntro(int id) {
        return userMapper.getUserIntroByOwnerId(id);
    }


    public User selectUserById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public User selectByOpenId(String openId) {
        return userMapper.selectByOpenId(openId);
    }

    public int addPlace(Place place) {
        return placeMapper.insert(place);
    }

    public int deletePlace(int id) {
        return placeMapper.deleteByPrimaryKey(id);
    }

    public List<Place> findAllPlace(int userId) {
        return placeMapper.selectByUserId(userId);
    }

    public int updatePlaceInfo(int id, String name, String address, String imgUrl) {
        return placeMapper.updatePlaceInfo(id, name, address, imgUrl);
    }


    public int updateByPrimaryKey(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    public int updateSubscribeByOpenId(String openId, int subscribe) {
        return userMapper.updateSubscribeByOpenId(openId, subscribe);
    }

    public List<User> selectAllUserByColumn(User user) {
        return userMapper.selectAllUserByColumn(user);
    }


    public List<User> findTeacher(Integer userId) {
        return userMapper.findTeacher(userId);
    }
}
